from selenium import webdriver



def init_driver(browser_type="chrome", headless=False, implicit_wait=30, maximize=True, options=None):
    browser_type = browser_type.lower()

    if browser_type == "chrome":
        chrome_options = options if options else webdriver.ChromeOptions()
        if headless:
            chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')

        driver=webdriver.Chrome(options=chrome_options)



    elif browser_type == "firefox":
        firefox_options = options if options else webdriver.FirefoxOptions()
        if headless:
            firefox_options.add_argument('--headless')


        driver = webdriver.Firefox(options=firefox_options)


    else:
        raise ValueError(f"不支持的浏览器类型: {browser_type}")

    # 设置隐式等待时间
    driver.implicitly_wait(implicit_wait)

    # 最大化窗口
    if maximize:
        driver.maximize_window()

    return driver

